package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ciei.dpagm.common.enums.ErrorCodeEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.*;
import com.ciei.dpagm.mapper.ChipInterfaceMapper;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangyan
 * @date 2023/05/08
 */
@Service
public class ChipInterfaceService extends BaseService<ChipInterfaceMapper, ChipInterface> {

    @Autowired
    private ChipInterfacePinFunctionService chipInterfacePinFunctionService;

    @Autowired
    private ChipPinFunctionService chipPinFunctionService;

    @Autowired
    private ChipPinService chipPinService;

    @Autowired
    private ChipInterfaceParameterService chipInterfaceParameterService;


    /**
     * 获取列表
     * @param masterChipId 主控芯片id
     * @param interfaceTypeList
     * @return
     */
    public List<ChipInterface> getList(Integer masterChipId, List<String> interfaceTypeList){
        if(interfaceTypeList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ChipInterface> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChipInterface::getChipId,masterChipId);
        lambdaQueryWrapper.in(ChipInterface::getInterfaceType,interfaceTypeList);
        return list(lambdaQueryWrapper);
    }

    /**
     * 获取引脚和参数数据
     * @param webData
     * @return
     */
    public JSONObject getPinAndParameterData(JSONObject webData){
        Integer masterChipId = webData.getInteger("masterChipId");
        String resourceName = webData.getString("resourceName");
        Map<SFunction<ChipInterface, ?>, Object> fieldValueMap = new HashMap<>();
        fieldValueMap.put(ChipInterface::getChipId,masterChipId);
        fieldValueMap.put(ChipInterface::getResourceName,resourceName);
        List<ChipInterface> chipInterfaceList = findByProperties(fieldValueMap);
        if(chipInterfaceList.isEmpty()){
            return JsonUtil.getFail(ErrorCodeEnum.DATA_NOT_FOUND,"根据芯片id和资源名称找不到对应的芯片接口数据，请检查");
        }
        if(chipInterfaceList.size() > 1){
            return JsonUtil.getFail(ErrorCodeEnum.DUPLICATE_DATA,"根据芯片id和资源名称找到多条芯片接口数据，请检查");
        }
        ChipInterface chipInterface = chipInterfaceList.get(0);
        List<JSONObject> pinList = new ArrayList<>();
        List<JSONObject> parameterList = new ArrayList<>();
        List<ChipInterfacePinFunction> chipInterfacePinFunctionList = chipInterfacePinFunctionService.findByProperty(ChipInterfacePinFunction::getInterfaceId, chipInterface.getInterfaceId());
        List<String> pinNameList = chipInterfacePinFunctionList.stream().map(ChipInterfacePinFunction::getPinName).collect(Collectors.toList());
        List<ChipPinFunction> chipPinFunctionList = chipPinFunctionService.getList(masterChipId, pinNameList);
        List<Integer> pinIdList = chipPinFunctionList.stream().map(ChipPinFunction::getPinId).collect(Collectors.toList());
        List<ChipPin> chipPinList = chipPinService.findListByIds(pinIdList);
        for(ChipInterfacePinFunction chipInterfacePinFunction : chipInterfacePinFunctionList){
            JSONObject pinData = new JSONObject();
            pinData.fluentPut("masterChipInterfacePinFunctionId",chipInterfacePinFunction.getInterfacePinFunctionId())
                    .fluentPut("masterChipPinName",chipInterfacePinFunction.getPinName());
            List<Integer> pinIdListByName = chipPinFunctionList.stream().filter(c -> chipInterfacePinFunction.getPinName().equals(c.getPinName())).map(ChipPinFunction::getPinId).collect(Collectors.toList());
            List<ChipPin> chipPinListByName = chipPinList.stream().filter(c -> pinIdListByName.contains(c.getPinId())).collect(Collectors.toList());
            List<JSONObject> pinCodeOptionList = new ArrayList<>();
            for(ChipPin chipPin : chipPinListByName){
                JSONObject pinCodeOption = new JSONObject();
                pinCodeOption.fluentPut("value",chipPin.getPinCode())
                        .fluentPut("label",chipPin.getPinCode())
                        .fluentPut("pinId",chipPin.getPinId());
                pinCodeOptionList.add(pinCodeOption);
            }
            pinData.put("pinCodeOptionList",pinCodeOptionList);
            pinList.add(pinData);
        }
        List<ChipInterfaceParameter> chipInterfaceParameterList = chipInterfaceParameterService.findByProperty(ChipInterfaceParameter::getInterfaceId, chipInterface.getInterfaceId());
        for (ChipInterfaceParameter chipInterfaceParameter : chipInterfaceParameterList){
            JSONObject chipInterfaceParameterData = new JSONObject();
            chipInterfaceParameterData.fluentPut("masterChipInterfaceParameterId",chipInterfaceParameter.getInterfaceParameterId());
            chipInterfaceParameterData.put("parameterName",chipInterfaceParameter.getParameterName());
            chipInterfaceParameterData.put("dataType",chipInterfaceParameter.getDataType());
            chipInterfaceParameterData.put("validValues",chipInterfaceParameter.getValidValues());
            chipInterfaceParameterData.put("symbol",chipInterfaceParameter.getSymbol());
            parameterList.add(chipInterfaceParameterData);
        }
        JSONObject data = new JSONObject();
        data.fluentPut("pinList",pinList)
                .fluentPut("parameterList",parameterList);
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,data);
    }

    /**
     * 获取引脚编号下拉框数据
     * @param webData
     * @return
     */
    public JSONObject getPinCodeOptionData(JSONObject webData){
        Integer masterChipId = webData.getInteger("masterChipId");
        String resourceName = webData.getString("resourceName");
        Map<SFunction<ChipInterface, ?>, Object> fieldValueMap = new HashMap<>();
        fieldValueMap.put(ChipInterface::getChipId,masterChipId);
        fieldValueMap.put(ChipInterface::getResourceName,resourceName);
        List<ChipInterface> chipInterfaceList = findByProperties(fieldValueMap);
        if(chipInterfaceList.isEmpty()){
            return JsonUtil.getFail(ErrorCodeEnum.DATA_NOT_FOUND,"根据芯片id和资源名称找不到对应的芯片接口数据，请检查");
        }
        if(chipInterfaceList.size() > 1){
            return JsonUtil.getFail(ErrorCodeEnum.DUPLICATE_DATA,"根据芯片id和资源名称找到多条芯片接口数据，请检查");
        }
        ChipInterface chipInterface = chipInterfaceList.get(0);
        List<ChipInterfacePinFunction> chipInterfacePinFunctionList = chipInterfacePinFunctionService.findByProperty(ChipInterfacePinFunction::getInterfaceId, chipInterface.getInterfaceId());
        List<String> pinNameList = chipInterfacePinFunctionList.stream().map(ChipInterfacePinFunction::getPinName).collect(Collectors.toList());
        List<ChipPinFunction> chipPinFunctionList = chipPinFunctionService.getList(masterChipId, pinNameList);
        List<Integer> pinIdList = chipPinFunctionList.stream().map(ChipPinFunction::getPinId).collect(Collectors.toList());
        List<ChipPin> chipPinList = chipPinService.findListByIds(pinIdList);
        JSONObject pinData = new JSONObject();
        for(ChipInterfacePinFunction chipInterfacePinFunction : chipInterfacePinFunctionList){
            List<Integer> pinIdListByName = chipPinFunctionList.stream().filter(c -> chipInterfacePinFunction.getPinName().equals(c.getPinName())).map(ChipPinFunction::getPinId).collect(Collectors.toList());
            List<ChipPin> chipPinListByName = chipPinList.stream().filter(c -> pinIdListByName.contains(c.getPinId())).collect(Collectors.toList());
            List<JSONObject> pinCodeOptionList = new ArrayList<>();
            for(ChipPin chipPin : chipPinListByName){
                JSONObject pinCodeOption = new JSONObject();
                pinCodeOption.fluentPut("value",chipPin.getPinCode())
                        .fluentPut("label",chipPin.getPinCode());
                pinCodeOptionList.add(pinCodeOption);
            }
            pinData.put(chipInterfacePinFunction.getPinName(),pinCodeOptionList);
        }
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,pinData);
    }

    /**
     * 获取列表
     * @param masterChipIdList
     * @return
     */
    public List<ChipInterface> getList(List<Integer> masterChipIdList){
        if(masterChipIdList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ChipInterface> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ChipInterface::getChipId,masterChipIdList);
        return list(lambdaQueryWrapper);
    }


}
